package com.project.service.impl;

import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.ILogMapper;
import com.project.mapper.IUserMapper;
import com.project.service.ILogService;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.List;

@Service//声明该类中是一个Spring服务组件
@Transactional//声明该类中的所有方法都支持事务管理
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private ILogMapper logMapper;
    @Override
    public UserBean login(String username, String password) {
        UserBean user = userMapper.login(username, password);
        if (user!=null){
            String time = LocalTime.now().toString();
            logMapper.add(new LogBean("用户"+user.getUsername()+"在"+time.substring(0,time.lastIndexOf(".")) +"登陆系统"));
        }
        return user;
    }

    @Override
    public boolean add(UserBean user) {
        if(userMapper.findByName(user.getUsername())!=null){
            return false;
        }
      userMapper.add(user);
        return true;
    }

    @Override
    public void del(Integer userId) {
        userMapper.del(userId);
    }

    @Override
    public void updateGrade(Integer id, String newGrade) {
    userMapper.updateGrade(id,newGrade);
    }

    @Override
    public UserBean findById(Integer userId) {
        return userMapper.findById(userId);
    }

    @Override
    public List<UserBean> findByGrade(String grade) {
        return userMapper.findByGrade(grade);
    }
}
